package com.qdu.servlet;

import com.google.gson.Gson;
import com.oreilly.servlet.MultipartRequest;
import com.qdu.util.CustomFileRenamePolicy2;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 该Servlet负责处理上传多张图片的请求
 *
 * @author Anna
 */
@WebServlet("/umps2")
public class UploadMultiplePhotosServlet2 extends HttpServlet {

    private String save_directory = "D:\\Typora Img\\photo";
    private Gson gson = new Gson();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        CustomFileRenamePolicy2 policy2=new CustomFileRenamePolicy2();
        
        MultipartRequest request = new MultipartRequest(req, save_directory, 100 * 1024 * 1024, "UTF-8",policy2);
        
        //调用CustomFileRenamePolicy2类的getFileNames()方法获取上传的多个文件的新名称（重命名后的名字）
        List<String> fileNames=policy2.getFileNames();
        
        System.out.println("---------------------------------------------------");
        
        for(String name:fileNames){
            System.out.println(name);
        }
        System.out.println("---------------------------------------------------");
        resp.setContentType("application/json;charset=UTF-8");
        String fileNamesJson = gson.toJson(fileNames);
        resp.getWriter().write(fileNamesJson);
        System.out.println(fileNamesJson);
    }
}
